package com.lyw.GeoLocationClient.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.chatui.DemoHXSDKHelper;
import com.lyw.GeoLocationClient.App;
import com.lyw.GeoLocationClient.Config;
import com.lyw.GeoLocationClient.R;


public class WelcomeActivity extends Activity {
    private TextView mMsg;

    private int ADMIN_LOGIN = 0;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            if (what == ADMIN_LOGIN) {
                if (App.getSpUtil().isNeedAdminRegisted()) {
                    startAdminRegisterActivity();
                } else {
                    startLoginActivity();
                }

                finish();
            }
        }
    };

	Runnable startAct = new Runnable() {

		@Override
		public void run() {
            if (App.isAdmin()) {
                mMsg.setText(getResources().getText(R.string.welcomeActivity));
                handler.sendEmptyMessageDelayed(ADMIN_LOGIN, 500);
                //get group list

            } else {
                mMsg.setText(getResources().getText(R.string.get_user_list));
                //get user imei list
                startLoginActivity();
                finish();
            }
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new Runnable() {
            public void run() {
                if (DemoHXSDKHelper.getInstance().isLogined()) {
                    // ** 免登陆情况 加载所有本地群和会话
                    //不是必须的，不加sdk也会自动异步去加载(不会重复加载)；
                    //加上的话保证进了主页面会话和群组都已经load完毕
                    long start = System.currentTimeMillis();
                    EMGroupManager.getInstance().loadAllGroups();
                    EMChatManager.getInstance().loadAllConversations();
                    long costTime = System.currentTimeMillis() - start;

                }else {

                }
            }
        }).start();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        mMsg = (TextView) findViewById(R.id.splash_msg);
//        App.checkAdminImei();


        handler.post(startAct);
    }

    private void startLoginActivity() {
        startActivity(new Intent(this, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    private void startAdminRegisterActivity() {
        startActivity(new Intent(this, AdminRegisterActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    /**
     * 获取当前应用程序的版本号
     */
    private String getVersion() {
        String st = getResources().getString(R.string.Version_number_is_wrong);
        PackageManager pm = getPackageManager();
        try {
            PackageInfo packinfo = pm.getPackageInfo(getPackageName(), 0);
            String version = packinfo.versionName;
            return version;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return st;
        }
    }
}
